package com.nit.lab.service.Impl;

import com.nit.lab.dao.UserDao;
import com.nit.lab.dto.DtoMenu;
import com.nit.lab.dto.utils.DtoDataTable;
import com.nit.lab.dto.DtoUser;
import com.nit.lab.model.SysUserEntity;
import com.nit.lab.service.UserService;
import com.nit.lab.utils.SystemConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Shaury
 * @date 2017/10/10
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;


    @Override
    public List<DtoUser> getUser4DataTable(DtoDataTable dtoDataTable) throws Exception {
        List<DtoUser> result = new ArrayList<DtoUser>();
        List<SysUserEntity> listTb = new ArrayList<SysUserEntity>();
        try {
            listTb = this.userDao.getUser4DataTable(dtoDataTable.getStart(), dtoDataTable.getLength(),
                    dtoDataTable.getSearch());
            if (listTb != null) {
                for (SysUserEntity tmp : listTb) {
                    result.add(new DtoUser(tmp));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Integer getUser4DataTableCount(String search) throws Exception {
        Integer result = 0;
        try {
            result = this.userDao.getUser4DataTableCount(search);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    @Transactional
    public Integer insertUserInfo(DtoUser dtoUser) throws Exception {
        Integer result = SystemConstants.Insert_Default;
        try {
            result = this.userDao.insertUserInfo(dtoUser.toBEAN());
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    @Transactional
    public Integer updateUserInfo(DtoUser dtoUser) throws Exception {
        Integer result = SystemConstants.Update_Success;
        try {
            result = this.userDao.updateUserInfo(dtoUser.toBEAN());
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return result;
    }
    @Override
    public String deleteUserInfo(Integer userId) throws Exception {
//        boolean hasContent = this.userDao.isUserHasContent(userId);
//        if(hasContent == false)
//        {
            return this.userDao.deleteUserInfo(userId);
//        } else {
//            return "HAS_CONTENT";
//        }
    }

    @Override
    public List<DtoMenu> getMenus(Integer actorId) throws Exception {
        DtoMenu dtoMenu;
        List<DtoMenu> results = new ArrayList<DtoMenu>();
        String menusId = userDao.getMenuId(actorId);
        String[] menList = menusId.split(",");
        for (int i = 0;i<menList.length;i++){
            dtoMenu = userDao.getMenus(Integer.parseInt(menList[i]));
            results.add(dtoMenu);
        }
        return results;

    }
}
